package com.library.view;

import com.library.controller.UserController;
import com.library.pojo.dto.user.UserLoginDTO;
import com.library.pojo.dto.user.UserRegisterDTO;
import com.library.pojo.entity.LibraryUsers;
import com.library.result.Result;
import com.library.utils.LocalCacheUtil;
import com.library.view.components.TipDialog;
import io.yukk1o.easytermui.EasyTermUI;
import io.yukk1o.easytermui.component.Button;
import io.yukk1o.easytermui.component.InputBox;
import io.yukk1o.easytermui.component.Panel;
import io.yukk1o.easytermui.component.Text;
import io.yukk1o.easytermui.util.PrintUtils;

import java.awt.*;

/**
 * 登录 / 注册界面
 *
 * 包含组件:
 * 中间区域：账号输入框（支持用户名/手机号）、密码输入框（隐藏明文）
 * 底部区域：登录按钮、注册按钮
 *
 * 交互流程:
 * 1. 登录：输入账号/密码 → 点击登录 → 校验通过跳转主菜单，失败弹窗提示；
 * 2. 注册：输入账号/密码 → 点击注册 → 成功提示后保留账号，失败弹窗提示。
 */



public class LoginAndRegionPanel {
    /// 创建EasyTermUI实例
    private final EasyTermUI easyTermUI = new EasyTermUI();

    public LoginAndRegionPanel() {
        // 创建用户控制器
        UserController userController = new UserController();
        // 创建根面板（整个终端界面）
        Panel rootPanel = new Panel(0, 0, 120, 40);

        // 标题文本
        Text line1 = new Text(0, 1, 120, 1, "========================================================================================================================");
        Text title = new Text(57, 3, 5, 1, "图书馆系统");
        Text line2 = new Text(0, 5, 120, 1, "========================================================================================================================");
        rootPanel.addComponent(line1);
        rootPanel.addComponent(title);
        rootPanel.addComponent(line2);

        Text subtitle = new Text(57, 9, 30, 1, "登录 / 注册");

        rootPanel.addComponent(subtitle);

        // 账号输入框
        Text usernameLabel = new Text(44, 13, 6, 1, "账号");
        InputBox usernameInput = new InputBox(51, 13, 25);
        rootPanel.addComponent(usernameLabel);
        rootPanel.addComponent(usernameInput);

        // 密码输入框
        Text passwordLabel = new Text(44, 15, 6, 1, "密码");
        InputBox passwordInput = new InputBox(51, 15, 25);
        rootPanel.addComponent(passwordLabel);
        rootPanel.addComponent(passwordInput);

        // 登录按钮
        Button loginButton = new Button(51, 19, "登录", (button) -> {
            String username = usernameInput.getValue();
            String password = passwordInput.getValue();
            // 登录逻辑
            Result<LibraryUsers> result = userController.login(new UserLoginDTO(username, password));
            if (result.getCode() == 1) {
                LocalCacheUtil.saveToCache("userId", String.valueOf(result.getData().getUserId()));
                LocalCacheUtil.saveToCache("role", String.valueOf(result.getData().getIsAdmin()));
                new MenuPanel();
                show();
            } else {
                easyTermUI.mount(new TipDialog(43, 10, "登录失败"));
            }
        });
        rootPanel.addComponent(loginButton);

        // 注册按钮
        Button registerButton = new Button(67, 19, "注册", (button) -> {
            String username = usernameInput.getValue();
            String password = passwordInput.getValue();
            // 注册逻辑
            Result<String> result = userController.register(new UserRegisterDTO(username, password));
            if (result.getCode() == 1) {
                Result<LibraryUsers> login = userController.login(new UserLoginDTO(username, password));
                LocalCacheUtil.saveToCache("userId", String.valueOf(login.getData().getUserId()));
                LocalCacheUtil.saveToCache("role", String.valueOf(login.getData().getIsAdmin()));
                new MenuPanel();
                show();
            } else {
                // 注册失败，弹窗提示
                easyTermUI.mount(new TipDialog(43, 10, "注册失败"));
            }
        });
        rootPanel.addComponent(registerButton);

        // 初始化界面
        PrintUtils.clearScreen();
        easyTermUI.init(rootPanel);
    }

    public void show() {
        PrintUtils.clearScreen();
        easyTermUI.start();
    }
}

